package cn.bdqn.asecu.service;

import cn.bdqn.asecu.utility.Utility_Provincial;
import cn.bdqn.asecu.utility.Utility_ru;
import cn.bdqn.pojo.Audit;
import cn.bdqn.pojo.Department;
import cn.bdqn.pojo.Role;
import cn.bdqn.pojo.User;
import cn.bdqn.repository.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProvincialService {

    @Resource
    private Asecu_UserRepository asecu_userRepository;
    @Resource
    private Asecu_DepartmentRepository asecu_departmentRepository;
    @Resource
    private Asecu_AuditRepository asecu_auditRepository;
    @Resource
    private Asecu_jurisdictionRoepository asecu_jurisdictionRoepository;

    public List<Department> Sel_Department_data(){
        List<Department> all = asecu_departmentRepository.findAll();
        return all;
    }

    public List<User> Userid(Integer id){
        Specification<User> spec=new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                return cb.or(cb.equal(root.get("departmentId"),id));
            }
        };
        return asecu_userRepository.findAll(spec);
    }

    public List<Audit> audit_id(){
        Specification<Audit> spec=new Specification<Audit>() {
            @Override
            public Predicate toPredicate(Root<Audit> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                return cb.or(cb.equal(root.get("parent"),0));
            }
        };

        List<Audit> all = asecu_auditRepository.findAll(spec);
        return all;
    }

    public Department Seldepa(Integer id){
        return asecu_departmentRepository.findById(id).get();
    }

    public Audit Selaudit_id(Integer id){
       return asecu_auditRepository.findById(id).get();
    }

    public List<Utility_Provincial>  departmentalUsers(Integer id,Integer bid){
        List<Utility_Provincial> list=new ArrayList<>();
        if(id!=0){
            List<Department> all=null;
            if (id!=-1) {
                Specification<Department> spec = new Specification<Department>() {
                    @Override
                    public Predicate toPredicate(Root<Department> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                        return cb.or(cb.equal(root.get("auditId"), id));
                    }
                };

                all = asecu_departmentRepository.findAll(spec);
            }else {
                all=asecu_departmentRepository.findAll();
            }

            for (Department d:all) {
                Utility_Provincial u=new Utility_Provincial();
                u.setDepartment(d);
                u.setUser(Userid(d.getId()));
                list.add(u);
            }
        }

        if (bid!=0) {
            Utility_Provincial u=new Utility_Provincial();
            Department department = asecu_departmentRepository.findById(bid).get();
            List<User> userid = Userid(department.getId());
            u.setDepartment(department);
            u.setUser(userid);
            list.add(u);
        }

            return list;
    }

    public void depaAdd(Department department){
        Department save = asecu_departmentRepository.save(department);
    }

    public void delremove(int id){
        asecu_departmentRepository.deleteById(id);
    }

    public List<Utility_ru> Selrole_user(Integer id){
        List<User> all=null;
        if (id!=0) {
            Specification<User> spec = new Specification<User>() {
                @Override
                public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                    return cb.or(cb.equal(root.get("roleId"), id));
                }
            };
            all = asecu_userRepository.findAll(spec);
        }else {
            all = asecu_userRepository.findAll();
        }
        List<Utility_ru> list=new ArrayList<>();
        for (User u:all) {
            Utility_ru utility_ru=new Utility_ru();
            utility_ru.setUser(u);
            utility_ru.setDepartment(Seldepa(u.getDepartmentId()));
            list.add(utility_ru);
        }

        return list;
    }

    public List<Role> Sel_role(){
        return asecu_jurisdictionRoepository.findAll();
    }

    public void add_role(Role role){
        Role save = asecu_jurisdictionRoepository.save(role);
    }

    public Role SelIdrole(Integer id){
        Role role = asecu_jurisdictionRoepository.findById(id).get();
        return role;
    }
}
